package nb.diskfilesystem.ui;

import javafx.animation.FadeTransition;
import javafx.animation.Interpolator;
import javafx.animation.ParallelTransition;
import javafx.animation.TranslateTransition;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TreeItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.*;
import javafx.util.Duration;
import nb.diskfilesystem.controller.FileSystemController;
import nb.diskfilesystem.model.DirectoryEntry;
import nb.diskfilesystem.model.DirectoryTree;

public class FileContextMenu {
    private ContextMenu blockContextMenu = new ContextMenu();
    private ContextMenu fileContextMenu = new ContextMenu();
    private ScrollPane scrollPane;
    private TilePane fileArea;
    private int fileCounter = 1; // 文件计数器
    private int folderCounter = 1; // 文件夹计数器
    private DirectoryTree directoryTree;
    private FileItem clickedFileItem; // 当前右键点击的 FileItem

    public FileContextMenu(DirectoryTree directoryTree) {
        this.directoryTree = directoryTree;
    }

    public void init() {

        scrollPane =MainView.scrollPane;
        fileArea = (TilePane) scrollPane.getContent();
        createBlockContextMenu();
        createFileContextMenu();
    }

    // 创建空白右键菜单
    private void createBlockContextMenu() {
        MenuItem menuItem1 = new MenuItem("新建文件");
        Image icon1 = new Image(getClass().getResourceAsStream("/nb/diskfilesystem/img/菜单文件.png"));
        ImageView iconView1 = new ImageView(icon1);
        iconView1.setFitWidth(20);  // 设置图标宽度
        iconView1.setFitHeight(20); // 设置图标高度
        iconView1.setTranslateX(-2.7); //
        menuItem1.setGraphic(iconView1);

        MenuItem menuItem2 = new MenuItem("新建文件夹");
        Image icon2 = new Image(getClass().getResourceAsStream("/nb/diskfilesystem/img/菜单文件夹.png"));
        ImageView iconView2 = new ImageView(icon2);
        iconView2.setFitWidth(17);  // 设置图标宽度
        iconView2.setFitHeight(17); // 设置图标高度
        menuItem2.setGraphic(iconView2);

        menuItem1.setOnAction(event -> createFile());
        menuItem2.setOnAction(event -> createFolder());
        blockContextMenu.getItems().addAll(menuItem1, menuItem2);
    }

    // 创建文件右键菜单
    private void createFileContextMenu() {
        MenuItem menuItem1 = new MenuItem("打开");
        Image icon1 = new Image(getClass().getResourceAsStream("/nb/diskfilesystem/img/打开.png"));
        ImageView iconView1 = new ImageView(icon1);
        iconView1.setFitWidth(20);  // 设置图标宽度
        iconView1.setFitHeight(20); // 设置图标高度
        iconView1.setTranslateX(-2.7);
        menuItem1.setGraphic(iconView1);

        MenuItem menuItem2 = new MenuItem("删除");
        Image icon2 = new Image(getClass().getResourceAsStream("/nb/diskfilesystem/img/删除.png"));
        ImageView iconView2 = new ImageView(icon2);
        iconView2.setFitWidth(20);  // 设置图标宽度
        iconView2.setFitHeight(20); // 设置图标高度
        iconView2.setTranslateX(-3);
        menuItem2.setGraphic(iconView2);

        MenuItem menuItem3 = new MenuItem("重命名");
        Image icon3 = new Image(getClass().getResourceAsStream("/nb/diskfilesystem/img/重命名.png"));
        ImageView iconView3 = new ImageView(icon3);
        iconView3.setFitWidth(18.5);  // 设置图标宽度
        iconView3.setFitHeight(18.5); // 设置图标高度
        iconView3.setTranslateX(-2.7);
        menuItem3.setGraphic(iconView3);

        MenuItem menuItem4 = new MenuItem("属性");
        Image icon4 = new Image(getClass().getResourceAsStream("/nb/diskfilesystem/img/属性.png"));
        ImageView iconView4 = new ImageView(icon4);
        iconView4.setFitWidth(20);  // 设置图标宽度
        iconView4.setFitHeight(20); // 设置图标高度
        iconView4.setTranslateX(-3);
        menuItem4.setGraphic(iconView4);

        menuItem1.setOnAction(event -> {
            if (clickedFileItem != null&&clickedFileItem.getIsFileOpen()==false) {
                clickedFileItem.openFileEditor(); // 调用 FileItem 的 openFile 方法
            }
        });

        menuItem3.setOnAction(event -> {
            if (clickedFileItem != null) {
                clickedFileItem.rename(); // 调用 FileItem 的 rename 方法
            }
        });

        fileContextMenu.getItems().addAll(menuItem1, menuItem2, menuItem3, menuItem4);
    }

    // 创建文件
    private void createFile() {
        TreeItem<String> selectedItem = directoryTree.getSelectedItem();
        String parentPath = selectedItem != null ? selectedItem.getValue() : "C盘";
        DirectoryEntry directoryEntry = new DirectoryEntry("文件(" + fileCounter++ + ")", "file", "txt", 0, 0);
        FileItem newFile = new FileItem(directoryEntry, "/nb/diskfilesystem/css/file.css", directoryEntry.getName(), this, fileArea);
        fileArea.getChildren().add(newFile);
        directoryTree.addItem(parentPath, directoryEntry); // 更新目录树
    }

    // 创建文件夹
    private void createFolder() {
        TreeItem<String> selectedItem = directoryTree.getSelectedItem();
        String parentPath = selectedItem != null ? selectedItem.getValue() : "C盘";
        DirectoryEntry directoryEntry = new DirectoryEntry("文件夹(" + folderCounter++ + ")", "folder", "txt", 0, 0);
        FileItem newFolder = new FileItem(directoryEntry, "/nb/diskfilesystem/css/folder.css", directoryEntry.getName(), this, fileArea);
        fileArea.getChildren().add(newFolder);
        directoryTree.addItem(parentPath, directoryEntry); // 更新目录树
    }

    //获取文件右键菜单
    public ContextMenu getFileContextMenu() {
        return fileContextMenu;
    }

    //获取空白处右键菜单
    public ContextMenu getBlockContextMenu() {
        return blockContextMenu;
    }

    // 显示文件右键菜单的方法
    public void showFileContextMenu(FileItem fileItem, double screenX, double screenY) {
        this.clickedFileItem = fileItem; // 设置当前右键点击的 FileItem
        fileContextMenu.show(fileItem, screenX, screenY);
    }

}
